//
//  JSRegisterViewController.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/17.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSRegisterViewController.h"
#import "JSSeetingUserInfoVC.h"
#import "JSWebViewController.h"
@interface JSRegisterViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentHeight;
@property (weak, nonatomic) IBOutlet QMUITextField *emailTF;
@property (weak, nonatomic) IBOutlet QMUITextField *userNameTF;
@property (weak, nonatomic) IBOutlet QMUITextField *pwdTF;
@property (weak, nonatomic) IBOutlet QMUITextField *checkTF;
@property (weak, nonatomic) IBOutlet UIButton *selectProtocBtn;
@property (weak, nonatomic) IBOutlet UILabel *protocDecL;
@property (weak, nonatomic) IBOutlet UIButton *protoctBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *space1;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *space2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *space3;

@end

@implementation JSRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didInitialized{
    [super didInitialized];
    self.view.backgroundColor = UIColorWhite;
    self.contentHeight.constant = kRealValue(44);
    self.emailTF.placeholder = JSLocalizedString(@"registeraccount");
    self.userNameTF.placeholder = JSLocalizedString(@"registername");
    self.pwdTF.placeholder = JSLocalizedString(@"registerapwd");
    self.checkTF.placeholder = JSLocalizedString(@"registeracheckpwd");
    self.protocDecL.text = JSLocalizedString(@"registerprotocdec");
    [self.protoctBtn setTitle:JSLocalizedString(@"registeraprotocl") forState:UIControlStateNormal];
    [self.protocDecL sizeToFit];
    [self.confirmBtn setTitle:JSLocalizedString(kLocalizableAlertOk) forState:UIControlStateNormal];
    self.space1.constant = self.space2.constant = self.space3.constant = kRealValue(20);
    
    RACSignal *enableSignal = [[RACSignal combineLatest:@[self.emailTF.rac_textSignal,self.userNameTF.rac_textSignal,self.pwdTF.rac_textSignal,self.checkTF.rac_textSignal]] map:^id(id value) {
        NSString *s1 = value[0];
        NSString *s2 = value[1];
        NSString *s3 = value[2];
        NSString *s4 = value[3];
        return @(JSRegularIsEmailQualified(s1) && s2.length && s3.length > 5 && s3.length < 12 && [s3 isEqualToString:s4]);
    }];
    self.confirmBtn.rac_command = [[RACCommand alloc] initWithEnabled:enableSignal signalBlock:^RACSignal *(id input) {
        return [RACSignal empty];
    }];
}
- (void)setNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated{
    [super setNavigationItemsIsInEditMode:isInEditMode animated:animated];
    self.title = JSLocalizedString(kLocalizableRegister);
}
- (IBAction)selectProtocClick:(UIButton *)sender {
    sender.selected = !sender.selected;
}
- (IBAction)seeProtocClick:(UIButton *)sender {
    JSWebViewController *vc = [[JSWebViewController alloc] init];
    vc.urlString = @"www.baidu.com";
    JS_VC_PUSHVC_ANIMATION(YES);
}
- (IBAction)confirmClick:(UIButton *)sender {
    if (!self.selectProtocBtn.selected) {
        [JSAlertHelper showFailHideAfterDelay:2 text:JSLocalizedString(@"alertProtoclDes") inView:self.view animation:YES];
        return;
    }
//
//    JSSeetingUserInfoVC *vc = [[JSSeetingUserInfoVC alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
    NSMutableDictionary *params = @{@"username":self.userNameTF.text,@"email":self.emailTF.text,@"password":self.pwdTF.text,@"repassword":self.checkTF.text}.mutableCopy;
    [MBManager showLoading];
    [JSNetManager js_courrentProjectRequestWithMouth:@"register" cache:NO ifShowFinishStatu:NO parameters:params successBlock:^(id response) {
        if ([self.view isShowingOnKeyWindow]) {
            if ([response[@"status"] integerValue] == 90001) {
                [JSAlertHelper showFailHideAfterDelay:2 text:JSLocalizedString(@"register90001") inView:self.view animation:YES];
            }else if ([response[@"status"] integerValue] == 200){
                // 注册成功后 设置个人信息
                JSSeetingUserInfoVC *vc = [[JSSeetingUserInfoVC alloc] init];
                vc.rigsterInfo = params;
                vc.rigsterInfo[@"token"] = response[@"data"][@"token"];
                [self.navigationController pushViewController:vc animated:YES];
            }else if ([response[@"status"] integerValue] == 90007){
                [JSAlertHelper showFailHideAfterDelay:2 text:JSLocalizedString(@"register90007") inView:self.view animation:YES];
            }
        }
    } failureBlock:^(NSError *error) {
    }];
}



@end
